#!/usr/bin/perl
# -*- mode: perl; -*-
# $Id: rgrep.devel.pl,v 1.6 2002/09/19 04:09:50 jimt Exp $
#
# 'rgrep'
#
# Basically, an application surrounding other apps that makes searching
# for stuff in nested directories a bit easier.
#
$program = "rgrep";
$version = "1.1";
#########################################################
#
# Some shell commands needed to make this work.  If they are not
# in your PATH, edit these variables to include the full path.
$grep = "grep";
$find = "find";
#
#########################################################
use Getopt::Std;
getopts('v:achijnrw') or printHelp( "invalid switch" );
printHelp() if $opt_h;

printHelp( "-a cannot be used with -j" ) if( $opt_a && $opt_j );
printHelp( "-c requires -v to be used" ) if( $opt_c && !$opt_v );
printHelp( "Incorrect number of arguments... 2 required" ) if( $#ARGV != 1 );

# Define $path and $string
my $string = $ARGV[0];
my $path   = $ARGV[1];

printHelp( "no such directory [$path]" ) if( !(-d $path) );

my $find_command = "$find $path -print";

if( $opt_v )
{
    my $v_string = "";
    ( @v_args ) = split( /,/, $opt_v );
    
    foreach( @v_args )
    {
	$v_string .= "$_|";
    }

    chop( $v_string );

    my $grepCmd = "$grep -vE";
    $grepCmd   .= "i" if( $opt_c );

    $find_command = join( " ", $find_command, "|", $grepCmd, "\"$v_string\"" ); 
}

my @files = `$find_command`;
foreach $file ( @files )
{
    chomp( $file );
    print "[checking] $file\n" if $opt_r;

    my $grepCmd = "$grep";
    $grepCmd   .= " -" if( $opt_i || $opt_w || ($opt_n && $opt_a) );
    $grepCmd   .= "i" if( $opt_i );
    $grepCmd   .= "w" if( $opt_w );
    $grepCmd   .= "n" if( $opt_n && $opt_a );

    @this_result = `$grepCmd "$string" "$file"`;
    if( $#this_result >= 0 )
    {
	if( $opt_j )
	{
	    print "$file\n";
	}
	else
	{		
	    print "['$string' found] $file\n";
	    if( $opt_a )
	    {
		foreach( @this_result )
		{
		    print $_;
		}

		print "\n";
	    }
	}
    }
}
#########################################################


sub printHelp
{
    my $error = shift;

    print "\n$error\n\n" if( $error );
    
    print << "ENDHelp";
-Usage: $program search_string path_to_search [-v string[,string] [-c]] [-r] 
        [-a] [-j] [-w] [-n] [-h] [-i]

    -a -> Displays all matching lines of 'search_string' found in a file.
    -v -> Remove files or directories matching 'string' as a substring, 
	  multiple options separated by commas.  Case-sensitive by default.
    -c -> Make -v case-insensitive.
    -r -> Report each file being tested
    -i -> Makes the search for 'search_string' case-insensitive.
    -j -> Returns just the path to the filename found.  No fluff.  
	  Not compatible with -a.
    -n -> Prints the line number of the matching line.  Only applicable 
	  with -a.
    -w -> Matches 'search_string' only if it is an exact, separate word 
	  ( grep-style behavior ).
    -h -> This message/help screen.

ENDHelp

    exit();
}
#########################################################
